﻿

<script>
    // details
    function ViewSong(ID)
    {
        document.getElementById("songID").value = ID;
        document.getElementById("ViewSong").click();
    }
    function ViewAlbum(ID)
    {
        document.getElementById("View_AlbumID").value = ID;
        document.getElementById("ViewAlbum").click();
    }

    // admin rights (delete is in its own)
    function EditSong(ID) {
        document.getElementById("Edit_SongID").value = ID;
        document.getElementById("EditSong").click();
    }

    function EditAlbum(ID)
    {
        document.getElementById("Edit_AlbumID").value = ID;
        document.getElementById("EditAlbum").click();
    }


    // buy
    var getType; //1: song, 2: album
    var getID;

    function TransactionCart(type, ID)
    {
        checked = document.getElementById(ID).checked;
        
        getType = type;
        getID = ID;

        if (checked) {
            AddToCart();
        }
        else {
            RemoveFromCart();
        }
    }
    function AddToCart() {
        XMLhttpAddToCart = new XMLHttpRequest();
        XMLhttpAddToCart.open("POST", "/Transaction/AddToCart", true);
        XMLhttpAddToCart.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        XMLhttpAddToCart.onreadystatechange = reqReadyAddToCart;
        sendStrAddToCart = "type=" + getType + "&productID=" + getID;
        XMLhttpAddToCart.send(sendStrAddToCart);
    }
    function reqReadyAddToCart() {
        if (XMLhttpAddToCart.readyState == 4 && XMLhttpAddToCart.status == 200) {
            if(XMLhttpAddToCart.response == "Please log in!" || XMLhttpAddToCart.response == "You bought this!")
            {
                alert(XMLhttpAddToCart.response);
                document.getElementById(getID).checked = false;
            }
            else
            {
                alert(XMLhttpAddToCart.response);
                customAlert(XMLhttpAddToCart.response, "2000");
            }

        } else if (XMLhttpAddToCart.readyState == 4) {
            alert("error! code: " + XMLhttpAddToCart.status);

        }
    }
    function RemoveFromCart() {
        XMLhttpRemoveFromCart = new XMLHttpRequest();
        XMLhttpRemoveFromCart.open("POST", "/Transaction/RemoveFromCart", true);
        XMLhttpRemoveFromCart.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        XMLhttpRemoveFromCart.onreadystatechange = reqReadyRemoveFromCart;
        sendStrRemoveFromCart = "type=" + getType + "&productID=" + getID;
        XMLhttpRemoveFromCart.send(sendStrRemoveFromCart);
    }    
    function reqReadyRemoveFromCart() {
        if (XMLhttpRemoveFromCart.readyState == 4 && XMLhttpRemoveFromCart.status == 200) {
            alert("Removed from cart!");

        } else if (XMLhttpRemoveFromCart.readyState == 4) {
            alert("error! code: " + XMLhttpRemoveFromCart.status);

        }
    }



    // wish
    function FavouriteCart(type, ID) 
    {
        getType = type;
        getID = ID;
        checked = document.getElementById("f" + ID).checked;
        if (checked) {
            AddToFav();
        }
        else {
            RemoveFromFav();
        }
    }
    function AddToFav() {
        XMLhttpAdd = new XMLHttpRequest();
        XMLhttpAdd.open("POST", "/WishList/AddToWish", true);
        XMLhttpAdd.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        XMLhttpAdd.onreadystatechange = reqReadyAddToFav;
        sendStrAdd = "type=" + getType + "&productID=" + getID;
        XMLhttpAdd.send(sendStrAdd);

    }
    function reqReadyAddToFav() {
        if (XMLhttpAdd.readyState == 4 && XMLhttpAdd.status == 200) {
            alert(XMLhttpAdd.response);
            if (XMLhttpAdd == "Please log in!")
                document.getElementById("f"+getID).checked = false;
        }
        else {
            if (XMLhttpAdd.readyState == 4) {
                alert("error! code: " + XMLhttpAdd.status);
            }

        }
    }
    function RemoveFromFav() {
        XMLhttpRemove = new XMLHttpRequest();
        XMLhttpRemove.open("POST", "/WishList/RemoveFromWish", true);
        XMLhttpRemove.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        XMLhttpRemove.onreadystatechange = reqReadyRemoveFromFav;
        sendStrRemove = "type=" + getType + "&productID=" + getID;
        XMLhttpRemove.send(sendStrRemove);
    }
    function reqReadyRemoveFromFav() {
        if (XMLhttpRemove.readyState == 4 && XMLhttpRemove.status == 200) {
            alert(XMLhttpRemove.response);

        } else if (XMLhttpRemove.readyState == 4) {
            alert("error! code: " + XMLhttpRemove.status);

        }
    }
    function UnWish(type, ID) {
        getType = type;
        getID = ID;
        XMLhttpUnwish = new XMLHttpRequest();
        XMLhttpUnwish.open("POST", "/WishList/RemoveFromWish", true);
        XMLhttpUnwish.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        XMLhttpUnwish.onreadystatechange = reqReadyUnWish;
        sendStrUnWish = "type=" + getType + "&productID=" + getID;
        XMLhttpUnwish.send(sendStrUnWish);
    }
    function reqReadyUnWish() {
        if (XMLhttpUnwish.readyState == 4 && XMLhttpUnwish.status == 200) {
            alert(XMLhttpUnwish.response);
            document.getElementById("refreshWish").click();

        } else if (XMLhttpUnwish.readyState == 4) {
            alert("error! code: " + XMLhttpUnwish.status);

        }
    }
</script>

@* To view album *@
<form action="/Products/ViewAlbum" method="post">
    <input type="text" id ="View_AlbumID" name ="albumID" hidden="hidden"/>
    <input type="submit" id="ViewAlbum" hidden="hidden"/>
</form>

@* To view song *@
<form action="/Products/ViewSong" method="post">
    <input type="text" id ="songID" name ="songID" hidden="hidden"/>
    <input type="submit" id="ViewSong" hidden="hidden"/>
</form>

@* To edit song *@
<form action="/Products/EditSong" method="post">
    <input type="text" id ="Edit_SongID" name ="songID" hidden="hidden"/>
    <input type="submit" id="EditSong" hidden="hidden"/>
</form>


@* To edit album *@
<form action="/Products/EditAlbum" method="post">
    <input type="text" id ="Edit_AlbumID" name ="albumID" hidden="hidden"/>
    <input type="submit" id="EditAlbum" hidden="hidden"/>
</form>

<form action="/WishList/Index" method="post">
    <input type="submit" id="refreshWish" hidden="hidden"/>
</form>
